/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.core.io;

import org.springframework.lang.Nullable;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

/**
 * Resource类是Spring用来封装IO处理的类。
 * <p>
 * Interface for a resource descriptor that abstracts from the actual
 * type of underlying resource, such as a file or class path resource.
 * 资源描述符的接口，该描述符从基础资源的实际类型中抽象出来，例如文件或类路径资源。
 * <p>
 * An InputStream can be opened for every resource if it exists in
 * physical form, but a URL or File handle can just be returned for
 * certain resources. The actual behavior is implementation-specific.
 * 如果InputStream以物理形式存在，则可以为每个资源打开InputStream，但仅可以为某些资源返回URL或File句柄。实际行为是特定于实现的。
 *
 * @author Juergen Hoeller
 * @see #getInputStream()
 * @see #getURL()
 * @see #getURI()
 * @see #getFile()
 * @see WritableResource
 * @see ContextResource
 * @see UrlResource
 * @see ClassPathResource
 * @see FileSystemResource
 * @see PathResource
 * @see ByteArrayResource
 * @see InputStreamResource
 * @since 28.12.2003
 */
public interface Resource extends InputStreamSource {
    /**
     * 确定该资源是否以物理形式实际存在
     * <p>
     * Determine whether this resource actually exists in physical form.
     * This method performs a definitive existence check, whereas the
     * existence of a {@code Resource} handle only guarantees a valid descriptor handle.
     */
    boolean exists();

    /**
     * 指示是否可以通过 org.springframework.core.io.InputStreamSource#getInputStream() 读取此资源的非空内容
     * <p>
     * Indicate whether non-empty contents of this resource can be read via {@link #getInputStream()}.
     * Will be {@code true} for typical resource descriptors that exist
     * since it strictly implies {@link #exists()} semantics as of 5.1.
     * Note that actual content reading may still fail when attempted.
     * However, a value of {@code false} is a definitive indication
     * that the resource content cannot be read.
     *
     * @see #getInputStream()
     * @see #exists()
     */
    default boolean isReadable() {
        return exists();
    }

    /**
     * 指示此资源是否表示具有打开流的句柄
     * <p>
     * Indicate whether this resource represents a handle with an open stream.
     * If {@code true}, the InputStream cannot be read multiple times,
     * and must be read and closed to avoid resource leaks.
     * Will be {@code false} for typical resource descriptors.
     */
    default boolean isOpen() {
        return false;
    }

    /**
     * 确定此资源是否代表文件系统中的文件
     * <p>
     * Determine whether this resource represents a file in a file system.
     * A value of {@code true} strongly suggests (but does not guarantee)
     * that a {@link #getFile()} call will succeed.
     * This is conservatively {@code false} by default.
     *
     * @see #getFile()
     * @since 5.0
     */
    default boolean isFile() {
        return false;
    }

    /**
     * 返回此资源的 URL 句柄
     * <p>
     * Return a URL handle for this resource.
     *
     * @throws IOException if the resource cannot be resolved as URL,
     *                     i.e. if the resource is not available as descriptor
     */
    URL getURL() throws IOException;

    /**
     * 返回此资源的 URI 句柄
     * <p>
     * Return a URI handle for this resource.
     *
     * @throws IOException if the resource cannot be resolved as URI,
     *                     i.e. if the resource is not available as descriptor
     * @since 2.5
     */
    URI getURI() throws IOException;

    /**
     * 返回此资源的 File 句柄
     * <p>
     * Return a File handle for this resource.
     *
     * @throws java.io.FileNotFoundException if the resource cannot be resolved as
     *                                       absolute file path, i.e. if the resource is not available in a file system
     * @throws IOException                   in case of general resolution/reading failures
     * @see #getInputStream()
     */
    File getFile() throws IOException;

    /**
     * Return a {@link ReadableByteChannel}.
     * It is expected that each call creates a <i>fresh</i> channel.
     * The default implementation returns {@link Channels#newChannel(InputStream)}
     * with the result of {@link #getInputStream()}.
     *
     * @return the byte channel for the underlying resource (must not be {@code null})
     * @throws java.io.FileNotFoundException if the underlying resource doesn't exist
     * @throws IOException                   if the content channel could not be opened
     * @see #getInputStream()
     * @since 5.0
     */
    default ReadableByteChannel readableChannel() throws IOException {
        return Channels.newChannel(getInputStream());
    }

    /**
     * 确定此资源的内容长度
     * <p>
     * Determine the content length for this resource.
     *
     * @throws IOException if the resource cannot be resolved
     *                     (in the file system or as some other known physical resource type)
     */
    long contentLength() throws IOException;

    /**
     * 确定此资源的最后修改时间戳
     * <p>
     * Determine the last-modified timestamp for this resource.
     *
     * @throws IOException if the resource cannot be resolved
     *                     (in the file system or as some other known physical resource type)
     */
    long lastModified() throws IOException;

    /**
     * 创建与此资源相关的资源
     * <p>
     * Create a resource relative to this resource.
     *
     * @param relativePath the relative path (relative to this resource)
     * @return the resource handle for the relative resource
     * @throws IOException if the relative resource cannot be determined
     */
    Resource createRelative(String relativePath) throws IOException;

    /**
     * 确定此资源的文件名，通常是路径的最后一部分，例如 myfile.txt
     * <p>
     * Determine a filename for this resource, i.e. typically the last part of the path: for example, "myfile.txt".
     * Returns {@code null} if this type of resource does not have a filename.
     */
    @Nullable
    String getFilename();

    /**
     * 返回此资源的描述，用于在使用资源时输出错误
     * <p>
     * Return a description for this resource, to be used for error output when working with the resource.
     * Implementations are also encouraged to return this value from their {@code toString} method.
     *
     * @see Object#toString()
     */
    String getDescription();
}
